package Controller;

import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import javax.swing.JFrame;

import view.DrawingArea;


public class MyWindow extends JFrame implements KeyListener{
	private static final long serialVersionUID = 1L;
	private ArrayList<Component> components;
	private GridBagLayout layout;
	public MyWindow(String title,int xSize,int ySize){
		super(title);
		
		layout = new GridBagLayout();
		components = new ArrayList<Component>();
		
		setLayout(layout);
		setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

		setSize(xSize,ySize);
		setMinimumSize(new Dimension(xSize,ySize));
		addKeyListener(this);
		setFocusable(true);

		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);	
	}
	
	// redraws window
	@SuppressWarnings("deprecation")
	public void redraw(){
		show();
		repaint();
	}
	
	// lets you add components to the window in the desired position
	public void addComponent(Component component, double x, double y, int posX, int posY) {
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.fill = GridBagConstraints.BOTH;
		
		constraints.gridx = posX;
		constraints.gridy = posY;
		
		constraints.weightx = x;
		constraints.weighty = y;
		
		components.add(component);
		add(component,constraints);
	}

	// changes packman animation and movment
	public void keyPressed(KeyEvent e) {
		char letter = e.getKeyChar();
		DrawingArea area = (DrawingArea) components.get(0);
		area.updatePack(letter);
	}

	// stops packman movment
	public void keyReleased(KeyEvent e) {
		DrawingArea area = (DrawingArea) components.get(0);
		area.stopPack();
	}
	public void keyTyped(KeyEvent e) {}
}
